package com.gpframework.module.controller.ctn;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.module.ctn.domain.GpCtnLoaded;
import com.gpframework.module.ctn.domain.GpCtnOrder;
import com.gpframework.module.ctn.domain.req.GpCtnLoadedAdd;
import com.gpframework.module.ctn.domain.req.GpCtnLoadedReq;
import com.gpframework.module.ctn.domain.vo.GpCtnLoadedVo;
import com.gpframework.module.ctn.service.IGpCtnLoadedService;
import com.gpframework.module.ctn.service.IGpCtnOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpCtnLoadedController
 * @Author gpframework
 * @Date 2024-01-08
 * @Version 1.0.0
 * @Description 装柜信息填写装柜录入Controller 接口类
 */
@Validated
@Api(tags = "装柜信息填写装柜录入")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/ctn/loaded" )
public class GpCtnLoadedController extends BaseController {

    private final IGpCtnLoadedService iGpCtnLoadedService;
    private final IGpCtnOrderService iGpCtnOrderService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-08
  * @Param gpCtnLoaded
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @GetMapping("/list")
    public TableDataInfo<GpCtnLoadedVo> list(GpCtnLoadedReq gpCtnLoadedReq)
    {
        startPage();
        QueryWrapper<GpCtnLoaded> lqw = new QueryWrapper<GpCtnLoaded>();
        gpCtnLoadedReq.generatorQuery(lqw,true);
        List<GpCtnLoadedVo> list = iGpCtnLoadedService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnLoaded
     * @Return AjaxResult
     */
    @ApiOperation("详情")
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpCtnLoadedVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpCtnLoadedService.getVoById(id));
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnLoaded
     * @Return AjaxResult
     */
    @ApiOperation("新增")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpCtnLoadedAdd gpCtnLoadedAdd) {
        GpCtnLoaded gpCtnLoaded = Convert.convert(new TypeReference<GpCtnLoaded>() {}, gpCtnLoadedAdd);
        GpCtnOrder gpCtnOrder = iGpCtnOrderService.getById(gpCtnLoadedAdd.getCtnOrderId());
        if (gpCtnOrder!=null){
            gpCtnLoaded.setAgentCtnNo(gpCtnOrder.getAgentCtnNo());
            gpCtnLoaded.setAgentCompany(gpCtnOrder.getAgentCompany());
            gpCtnLoaded.setBillNo(gpCtnOrder.getBillNo());
            gpCtnLoaded.setCtnNo(gpCtnOrder.getCtnNo());
            gpCtnLoaded.setSealNo(gpCtnOrder.getSealNo());
        }

        if (gpCtnLoadedAdd.getCtnLoadedId()==null){
            return toAjax(iGpCtnLoadedService.save(gpCtnLoaded) ? 1 : 0);
        }
        gpCtnLoaded.setCtnOrderId(gpCtnOrder.getId());
        return toAjax(iGpCtnLoadedService.updateById(gpCtnLoaded) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnLoaded
     * @Return AjaxResult
     */
    @ApiOperation("修改")
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpCtnLoadedAdd gpCtnLoadedAdd) {
        GpCtnLoaded gpCtnLoaded = Convert.convert(new TypeReference<GpCtnLoaded>() {}, gpCtnLoadedAdd);
        GpCtnOrder gpCtnOrder = iGpCtnOrderService.getById(gpCtnLoaded.getCtnOrderId());
        gpCtnLoaded.setCtnOrderId(gpCtnOrder.getId());
        gpCtnLoaded.setBillNo(gpCtnOrder.getBillNo());
        gpCtnLoaded.setCtnNo(gpCtnOrder.getCtnNo());
        gpCtnLoaded.setSealNo(gpCtnOrder.getSealNo());
        return toAjax(iGpCtnLoadedService.updateById(gpCtnLoaded) ? 1 : 0);
    }

    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnLoaded
     * @Return AjaxResult
     */
    @ApiOperation("删除")
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpCtnLoadedService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
